
* *********************************
* ****** Nationwide ***************
* *********************************

* Path: Nationwide

use "nationwide_95_13.dta", clear 
rename postcode post_code
rename llsoa_2001 lsoa_code
order post_code lsoa_code post_code
sort post_code
merge m:1 lsoa_code using "lsoa_nuclear.dta"
drop if _merge!=3
drop _merge
merge m:1 lsoa_code using "deprivation.dta"
drop if _merge==2
drop _merge
generate time_treatment=1 if year*100+month>201103
replace time_treatment=0 if year*100+month<=201103
global sample ""
generate name=name_first ${sample}
generate distance_nuclear=distance_first ${sample}
generate orientation_nuclear=orientation_first-_pi/4-_pi * floor((orientation_first-_pi/4)/_pi) ${sample}
generate capacity=capacity_first ${sample}
generate connection=connection_first ${sample}
generate expclosure=expclosure_first ${sample}
generate numberaccidents=numberaccidents_first ${sample}
global sample "if operating_second!="" & operating_first=="""
replace name=name_second ${sample}
replace distance_nuclear=distance_second ${sample}
replace capacity=capacity_second ${sample}
replace connection=connection_second ${sample}
replace expclosure=expclosure_second ${sample}
replace numberaccidents=numberaccidents_second ${sample}
generate treatment_powerplant=(min(distance_gas,distance_biomass,distance_ccgt,distance_coal)<20)
replace treatment_powerplant=.  if (min(distance_gas,distance_biomass,distance_ccgt,distance_coal)> 100)
generate treatment_nuclear_pp=1 if (distance_first<20)
replace treatment_nuclear_pp=0 if (distance_first>=20 & treatment_powerplant==1) 
generate treatment_nuclear=1 if (distance_first<20)
replace treatment_nuclear=0 if (distance_first>=20 & distance_first<100) 
foreach dist in 10 15 25 30 40 50 {
generate treatment_`dist'=1 if (distance_first<`dist')
replace treatment_`dist'=0 if (distance_first>=`dist' & distance_first<100) 
}
drop name_first name_second-numberaccidents_past_second
generate name_waste=location_firstw
generate distance_waste=distance_firstw
generate packvol_waste=packagedvolume_firstw
generate treatment_waste=1 if distance_firstw<20 & packvol_waste>0
replace treatment_waste=0 if (distance_firstw>=20 & distance_firstw<100) & packvol_waste>0
generate treatment_nuclearpro=1 if (distance_firstpro<20) & treatment_nuclear!=1
replace treatment_nuclearpro=0 if (distance_firstpro>=20 & distance_firstpro<100) 
egen group_nuclear=group(name)
egen group_waste=group(name_waste)
keep time_treatment treatment_nuclear treatment_waste month lsoa_code group_waste group_nuclear lnprice rank_deprivation_score year bedrooms floorsz bathroom rank_deprivation_score
order lsoa_code year month time_treatment treatment_nuclear treatment_waste group_nuclear group_waste
label var lsoa_code "LSOA code"
label var year "Year of transaction"
label var month "Month of transaction"
label var time_treatment "Dummy equal to 1 after April, 2011"
label var treatment_nuclear "Dummy equal to 1 if LSOA centroid within 20 kilometers of a nuclear plant"
label var treatment_waste "Dummy equal to 1 if LSOA centroid within 20 kilometers of a waste site" 
label var group_nuclear "Identifier of closest nuclear plant"
label var group_waste "Identifier of closest waste site"
label var rank_deprivation_score "Deprivation score (rank, 2010, LSOA)"
label var bathroom "Number of bathrooms (Nationwide)"
label var bedrooms "Number of bedrooms (Nationwide)"
label var floorsz "Square meters (Nationwide)"
label var lnprice "Price (log, Nationwide)"
save "final_nationwide.dta", replace
